package com.tlgen.modules.drugstore.controller;

import com.tlgen.common.exception.BadRequestException;
import com.tlgen.modules.drugstore.domain.MedicinalSort;
import com.tlgen.modules.drugstore.service.MedicinalSortService;
import com.tlgen.modules.drugstore.service.dto.MedicinalSortQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

@Api(tags = "系统：药品分类")
@RestController
@RequestMapping("/api/medicinalSort")
public class MedicinalSortController {

    private final MedicinalSortService medicinalSortService;

    private static final String ENTITY_NAME = "medicinalSort";

    public MedicinalSortController(MedicinalSortService medicinalSortService) {
        this.medicinalSortService = medicinalSortService;
    }

    @ApiOperation("返回全部的药品分类")
    @GetMapping(value = "/all")
    @PreAuthorize("@tlgen.check('medicinalSort:list')")
    public ResponseEntity<Object> getAll(@PageableDefault(value = 2000, sort = {"id"}, direction = Sort.Direction.ASC) Pageable pageable){
        return new ResponseEntity<>(medicinalSortService.queryAll(pageable), HttpStatus.OK);
    }

    @ApiOperation("查询")
    @GetMapping
    @PreAuthorize("@tlgen.check('medicinalSort:list')")
    public ResponseEntity<Object> getRecords(MedicinalSortQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(medicinalSortService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @ApiOperation("新增")
    @PostMapping
    @PreAuthorize("@tlgen.check('medicinalSort:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody MedicinalSort resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        return new ResponseEntity<>(medicinalSortService.create(resources), HttpStatus.CREATED);
    }

    @ApiOperation("修改")
    @PutMapping
    @PreAuthorize("@tlgen.check('medicinalSort:edit')")
    public ResponseEntity<Object> update(@Validated(MedicinalSort.Update.class) @RequestBody MedicinalSort resources){
        medicinalSortService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("删除")
    @DeleteMapping
    @PreAuthorize("@tlgen.check('medicinalSort:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
        medicinalSortService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@tlgen.check('medicinalSort:list')")
    public void download(HttpServletResponse response, MedicinalSortQueryCriteria criteria) throws IOException {
        medicinalSortService.download(medicinalSortService.queryAll(criteria), response);
    }

}
